package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsGroupon;
import com.qf.service.DtsGrouponService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @auther Administrator
 * @date 2023/2/25 9:30
 */
//@RequestMapping("/admin/groupon")
@RestController
@CrossOrigin
public class GrouponController {
    @Autowired
    private DtsGrouponService dtsGrouponService;


    /**
     * 团购规则显示
     * @param goodsId
     * @param sort
     * @param order
     * @param page
     * @param limit
     * @return
     */
    @RequiresPermissions(value = "admin:groupon:list")
    @RequiresPermissionsDesc(menu = {"推广管理","团购规则"},button = "查询")
    @GetMapping("/list")
    public Object list(String goodsId, String sort, String order,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit){

        IPage<DtsGroupon> iPage = dtsGrouponService.findListByQuery(goodsId,sort,order,page,limit);
        Map data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    /**
     * /create
     */
    @RequiresPermissions(value = "admin:groupon:create")
    @RequiresPermissionsDesc(menu = {"推广管理","团购规则"},button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsGroupon dtsGroupon){
        dtsGroupon = dtsGrouponService.create(dtsGroupon);
        return ResponseUtil.ok(dtsGroupon);

    }
}
